package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.activity;

import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.R;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.entity.Product;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.utils.ProductUtils;

/**
 * 从该活动跳转到其它活动一定要在startActivity后finish()
 */
public class ActivityImportFromFile extends AppCompatActivity {
    private List<Product> products;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_import_from_file);

        SharedPreferences sharedPreferences = this.getSharedPreferences("ACCOUNT_REMEMBER", MODE_PRIVATE);
        if (sharedPreferences.getInt("role",-1) != 0) {
            new AlertDialog.Builder(this)
                    .setTitle("注意！")
                    .setMessage("你没有这个权限！")
                    .setPositiveButton("确认",(dialog,which) -> finish())
                    .setCancelable(true)
                    .show();
        } else {
            Intent intent = getIntent();
            String action = intent.getAction();
            Uri fileUri = intent.getData();
            ContentResolver contentResolver = getContentResolver();

            products = new ArrayList<>();

            if (Intent.ACTION_VIEW.equals(action) && fileUri != null) {
                String mimeType = getContentResolver().getType(fileUri);
                int result;

                try {
                    if (mimeType != null) {
                        switch (mimeType) {
                            case "text/plain":
                                result = ProductUtils.importFromTextFile(products, contentResolver, fileUri);
                                break;
                            case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
                                result = ProductUtils.importFromExcelFile(products, contentResolver, fileUri);
                                break;
                            default:
                                new AlertDialog.Builder(this)
                                        .setTitle("注意！")
                                        .setMessage("不支持的文件。")
                                        .setPositiveButton("确认",(dialog,which) -> finish())
                                        .setCancelable(true)
                                        .show();
                                result = -1;
                        }

                        if (result > -1) {
                            new AlertDialog.Builder(this)
                                    .setTitle("注意！")
                                    .setMessage("文件格式错误：第 " + result + " 行。\n导入终止。")
                                    .setPositiveButton("确认", (dialog,which) -> finish())
                                    .setCancelable(true)
                                    .show();
                        } else {
                            new AlertDialog.Builder(this)
                                    .setTitle("导入成功！")
                                    .setMessage("是否跳转到管理页面并上传数据？")
                                    .setPositiveButton("是", (dialog, which) -> {
                                        Intent intentRedirectToApp = new Intent(ActivityImportFromFile.this, ActivityProducer.class);
                                        intentRedirectToApp.putExtra("productsImported", (Serializable) products);
                                        startActivity(intentRedirectToApp);
                                        finish();
                                    })
                                    .setNegativeButton("否", (dialog, which) -> finish())
                                    .setCancelable(true)
                                    .show();
                        }
                    } else {
                        // TODO OTHER SHARED FILE FORMAT JUDGEMENT
                    }
                } catch (IOException e) {
                    new AlertDialog.Builder(this)
                            .setTitle("注意！")
                            .setMessage("文件打开失败。")
                            .setPositiveButton("确认",(dialog,which) -> finish())
                            .setCancelable(true)
                            .show();
                    e.printStackTrace();
                }
            }
        }
    }
}
